package com.example.service.shopping;


import com.example.common.enums.RoleEnum;
import com.example.mapper.shopping.MedicinesStyleMapper;
import com.example.model.entity.other.Account;
import com.example.model.entity.shopping.MedicinesStyle;
import com.example.model.vo.MedicinesStyleVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 药品规格表业务处理
 **/
@Service
public class MedicinesStyleService {

    @Resource
    private MedicinesStyleMapper medicinesStyleMapper;

    /**
     * 新增
     */
    public void add(MedicinesStyle medicinesStyle) {
        medicinesStyleMapper.insert(medicinesStyle);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        medicinesStyleMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            medicinesStyleMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(MedicinesStyle medicinesStyle) {
        medicinesStyleMapper.updateById(medicinesStyle);
    }

    /**
     * 根据ID查询
     */
    public MedicinesStyle selectById(Integer id) {
        return medicinesStyleMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<MedicinesStyleVO> selectAll(MedicinesStyle medicinesStyle) {
        MedicinesStyleVO medicinesStyleVO=new MedicinesStyleVO();
        BeanUtils.copyProperties(medicinesStyle,medicinesStyleVO);
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            medicinesStyleVO.setBusinessId(currentUser.getId());
        }
        return medicinesStyleMapper.selectAll(medicinesStyleVO);
    }

    /**
     * 分页查询
     */
    public PageInfo<MedicinesStyleVO> selectPage(MedicinesStyle medicinesStyle, Integer pageNum, Integer pageSize) {
        MedicinesStyleVO medicinesStyleVO=new MedicinesStyleVO();
        BeanUtils.copyProperties(medicinesStyle,medicinesStyleVO);
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.BUSINESS.name().equals(currentUser.getRole())){
            medicinesStyleVO.setBusinessId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MedicinesStyleVO> list = medicinesStyleMapper.selectAll(medicinesStyleVO);
        return PageInfo.of(list);
    }

}
